//
//  FileAssistant.m
//  HiQ
//
//  Created by 庞孟羽 on 12-8-9.
//
//

#import "FileAssistant.h"

@implementation FileAssistant

+ (void)saveData:(NSString *)data selfQQ:(NSString*)sQQ fileName:(NSString*)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *providerPath = [NSString stringWithFormat:@"%@/HiQ/%@", path,sQQ];
    NSString *file = [providerPath stringByAppendingPathComponent:fileName];
    NSString *newData;
    if (![fileManager fileExistsAtPath:providerPath]) {
        [fileManager createDirectoryAtPath:providerPath withIntermediateDirectories:YES attributes:nil error:nil];
    }

    NSString *preData = [self readData:fileName selfQQ:sQQ];
    if (preData == nil) {
        newData = [NSString stringWithFormat:@"%@",data];
    }
    else
    {
        newData = [NSString stringWithFormat:@"%@\n%@",[self readData:fileName selfQQ:sQQ],data];
    }

    [NSKeyedArchiver archiveRootObject:newData toFile:file];
}


+ (NSString *)readData:(NSString*)fileName selfQQ:(NSString*)sQQ
{
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *providerPath = [NSString stringWithFormat:@"%@/HiQ/%@", path,sQQ];
    NSString *file = [providerPath stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:file]) {
        NSString *data = [NSKeyedUnarchiver unarchiveObjectWithFile: file];
        return data;
    }
    return nil;
}


@end
